<?php 
class Tourismgoodsbrand extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_goods_brand_model', 'tourism_goods_brand');
        $this->load->model('tourism_goods_category_model', 'tourism_goods_category');
    }
    
     /**
     * 品牌列表
     * @auther laona
     * test time 2015.5.7
     **/
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('tourismgoodsbrand/grid').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('tourismgoodsbrand/grid');
        $config['total_rows']  = $this->tourism_goods_brand->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list']   = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_goods_brand->page_list($page_num, $num, $getData);
        $data['all_rows']  = $config['total_rows'];
        $data['pg_now']    = $pg;
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismgoodsbrand/grid', $data);
    }

    /*
     * 品牌添加
     * @auther laona
     **/
    public function add()
    {
        $this->load->view('tourismgoodsbrand/add');
    }
    
    /*
     * 品牌添加处理
     * @auther laona
     **/
    public function addPost()
    {
        $error = array();
        if ($this->validateParam($this->input->post('brand_name'))) {
            $error = '品牌名称不能为空。';
        }
        if ($this->validateParam($this->input->post('class_c'))) {
            $error = '品牌所属不能为空。';
        }
        $postData = $this->input->post();
        $postData['category_name'] = $this->tourism_goods_category->getCategoryByLastId($this->input->post('class_c'));
        
        $this->db->trans_start();
        $resultId = $this->tourism_goods_brand->addBrand($postData);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodsbrand/grid', '', '保存成功！');
        } else {
            $this->error('tourismgoodsbrand/add', '', '保存失败！');
        }
    }
    
    /*
     * 品牌修改
     * @auther laona
     **/
    public function edit($brand_id)
    {
        $brand = $this->tourism_goods_brand->getBrandByBrandId($brand_id);
        $brand = $brand->row();
        $category_ids = $this->tourism_goods_category->getParamsId($brand->category_id);
        sort($category_ids);
        $data['brand'] = $brand;
        $data['class_a'] = isset($category_ids[0]) ? $category_ids[0] : '';
        $data['class_b'] = isset($category_ids[1]) ? $category_ids[1] : '';
        $data['class_c'] = isset($category_ids[2]) ? $category_ids[2] : '';
        $this->load->view('tourismgoodsbrand/edit', $data);
    }
    
    /*
     * 品牌修改处理
     * @auther laona
     **/
    public function editPost()
    {
        $error = array();
        if ($this->validateParam($this->input->post('brand_name'))) {
            $error = '品牌名称不能为空。';
        }
        if ($this->validateParam($this->input->post('class_c'))) {
            $error = '品牌所属不能为空。';
        }
        $postData = $this->input->post();
        $postData['category_name'] = $this->tourism_goods_category->getCategoryByLastId($this->input->post('class_c'));
        $this->db->trans_start();
        $resultId = $this->tourism_goods_brand->update($postData);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodsbrand/grid', '', '更新成功！');
        } else {
            $this->error('tourismgoodsbrand/add', '', '更新失败！');
        }
    }
    
     /**
     * 品牌删除
     * @auther laona
     **/
    public function delete($brand_id)
    {
        if (!$brand_id) {
            $this->error('tourismgoodsbrand/add', '', '删除失败！');
        }
        if ($this->tourism_goods_brand->delete($brand_id)) {
            $this->success('tourismgoodsbrand/grid', '', '删除成功！');
        } else {
            $this->error('tourismgoodsbrand/add', '', '删除失败！');
        }
    }
}